package dd.control;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JTextField;

import xml.Message;
import dd.model.Event;
import dd.model.Model;
import dd.view.CreateForm;
import dd.view.SignInScreen;


public class SignInButtonController implements ActionListener {
	/**
	 * call when user at the sign in screen click sign in button
	 * get the information on the screen which is username and password
	 * send the information to the server(difference between user and moderator)
	 */
	
	SignInScreen signin;
	Event event;
	
	public SignInButtonController(SignInScreen sis, Event event) {
		signin = sis;
		this.event = event;
	}
	
	public void actionPerformed(ActionEvent arg0) {
		
		JTextField usernameScreen = signin.getUserNameTextField();
		JTextField passwordScreen = signin.getPasswordTextField();
		String username = usernameScreen.getText();
		String password = passwordScreen.getText();
		Model model = Model.getInstance();
		Event e = model.getEvent();
		System.out.println("id want to login:" + e.getEventID());
		//send message depending on is this is moderator
		if(e.isModSignIn()){
			e.setUserName(username);
			e.setPassword(password);
			CreateForm creatform = new CreateForm(event);
			creatform.setVisible(true);				
			signin.dispose();
		}
		else{
			e.setUserName(username);
			e.setPassword(password);
		String xmlString = Message.requestHeader() + "<signInRequest id='"+e.getEventID() +"'>"+
				"<user name='"+ username + "' password='" + password + "'/>" +
				"</signInRequest>"+"</request>";
		Message m = new Message (xmlString);
		model.getServerAccess().sendRequest(m);
		//set signinscreen to model for furthur use
		model.setSignInScreen(signin);
		}

		
	/*	if(event.getUserName().equals(username) && event.getPassword().equals(password)) {
			event.setUserStatus();
			if(event.getMode().equals("Open")) {
				addChoiceScreen addchoiceScreen = new addChoiceScreen(event);
				addchoiceScreen.setVisible(true);				
			}
			else {
				addEdgesScreen addES = new addEdgesScreen(event);
				addES.setVisible(true);
			}
			signin.dispose();
		}
		else {
			System.out.println("User name or password is not correct, please check again!");
		}
		
		if(event.getModName().equals(username) && event.getModPass().equals(password)) {
			event.setModStatus();			
			CreateForm creatform = new CreateForm(event);
			creatform.setVisible(true);				
			signin.dispose();
		}
		else {
			System.out.println("User name or password is not correct, please check again!");
		}*/
		
		
	}

}
